package effectivejava.serializable;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class SerializableTest extends SerialzableMain implements Serializable{
	
	private static final long serialVersionUID = 6362594977182459112L;
	
	private int id;
	private String firstName;
	private String lastName;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	
	
	
	
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		
		SerializableTest sr = new SerializableTest();
		sr.setFirstName("Prateek");
		sr.setLastName("Rana");
		sr.setId(29);	
		sr.setDesignation("SEl");
		serialize("Test.ser",sr);
		
		SerializableTest sr1 = (SerializableTest)deSerialize("Test.ser");
		
		
		System.out.println(sr1.firstName);
		System.out.println(sr1.lastName);
		System.out.println(sr1.id);
		
		System.out.println(sr1.getDesignation());
	}
	
	public static void serialize(String outFile,Object serializableObject) throws IOException{
		 FileOutputStream fos = new FileOutputStream(outFile);
		    ObjectOutputStream oos = new ObjectOutputStream(fos);
		    oos.writeObject(serializableObject);
		
	}
	
	public static Object deSerialize(String serilizedObject)
		      throws FileNotFoundException, IOException, ClassNotFoundException {
		    FileInputStream fis = new FileInputStream(serilizedObject);
		    ObjectInputStream ois = new ObjectInputStream(fis);
		    return ois.readObject();
		  }

}
